package biggestloser.servlet;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import biggestloser.util.DatastoreUtil;

import com.google.appengine.api.datastore.Entity;

import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


/**
 * This servlet is invoked by the weekly cron to send weigh-in e-mails to
 * all users.
 */
public class EmailSender extends HttpServlet
{
	private static final long serialVersionUID = 3675111461254492650L;
	
	private static final Logger log = Logger.getLogger(EmailSender.class.getName());

	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
    {
		 long week = DatastoreUtil.getCurrentWeek();
		
		 // Get list of users from datastore		
		 List<Entity> users = DatastoreUtil.getUsers();
	     
		 // Send email to each user
	     for (Entity user : users)
		 {
	    	 String name  = user.getProperty("name").toString();
	    	 String email = user.getProperty("email").toString();
	    	 
	    	 // Send an email
	    	 Properties props = new Properties();
	         Session session = Session.getDefaultInstance(props, null);

	         String msgBody = "It's time for your weekly weigh in! Please respond to this message " +
	                          "with your weight for today.\n\n"  +
	        		 
	                          "Please respond with only a single line, containing only your " +
	                          "current weight. Do not add any extra characters.\n\n" +
	        		 
	        		          "As a reminder, your results are confidential and only used to calculate " +
	                          "the percentage change from the previous week. Nobody can see your responses. Good luck!\n\n" +
	         
	         				  "You can view everyone's current progress at http://lynxbiggestloser.appspot.com\n";
	         
	         try
	         {
	             Message msg = new MimeMessage(session);
	             msg.setFrom(new InternetAddress("admin@lynxbiggestloser.appspotmail.com", "Lynx Biggest Loser Admin"));
	             msg.addRecipient(Message.RecipientType.TO,
	                              new InternetAddress(email, name));
	             msg.setSubject("Biggest Loser Weekly Weigh In - Week " + week);
	             msg.setText(msgBody);

	             Transport.send(msg);
	             
	             log.info("Send weekly e-mail to " + name);
	         }
	         catch (AddressException e)
	         {
	             log.severe("Unable to send email to " + email);
	             e.printStackTrace();
	         }
	         catch (MessagingException e)
	         {
	        	 log.severe("Unable to send email to " + email);
	        	 e.printStackTrace();
	         }
		 }
    }
}